package api

import (
	"api_gateway/basic/config"
	__ "api_gateway/basic/proto"
	"api_gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateOrder(c *gin.Context) {
	var req request.CreateOrder
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "验证失败",
			"data":    err.Error(),
		})
		return
	}
	order, err := config.UserClient.CreateOrder(c, &__.CreateOrderReq{
		UserId:    req.UserId,
		DirverId:  req.DriverId,
		Start:     req.Start,
		End:       req.End,
		Kilometer: float32(req.Kilometer),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "创建订单失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "订单创建成功",
		"data":    order,
	})
}

func Update(c *gin.Context) {
	var req request.Update
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "验证失败",
			"data":    err.Error(),
		})
		return
	}
	order, err := config.UserClient.UpdateOrderStatus(c, &__.UpdateOrderStatusReq{
		Id:     int64(req.Id),
		Status: 2,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "更新失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "修改成功",
		"data":    order,
	})
}

func DetailOrder(c *gin.Context) {
	var req request.DetailOrder
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "验证失败",
			"data":    err.Error(),
		})
		return
	}
	detail, err := config.UserClient.OrderDetail(c, &__.OrderDetailReq{Id: int64(req.Id)})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "详情展示失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{
		"code":    200,
		"message": "详情展示成功",
		"data":    detail,
	})
}

func MyOrderList(c *gin.Context) {
	var req request.MyOrderList
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "验证失败",
			"data":    err.Error(),
		})
		return
	}
	list, err := config.UserClient.MyOrdersList(c, &__.MyOrdersListReq{UserId: int64(req.UserId)})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "我的订单展示失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{
		"code":    200,
		"message": "我的订单展示成功",
		"data":    list,
	})

}
